// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.gameStates;

import org.lwjglx.input.Keyboard;
import zombie.core.Core;
import zombie.core.SpriteRenderer;
import zombie.modding.ActiveMods;
import zombie.network.GameClient;
import zombie.ui.LoadingQueueUI;
import zombie.ui.UIManager;

public class LoadingQueueState extends GameState {
    private static boolean bCancel = false;
    private static boolean bDone = false;
    private static int placeInQueue = -1;
    private boolean bAButtonDown = false;
    private static final LoadingQueueUI ui = new LoadingQueueUI();

    @Override
    public void enter() {
        bCancel = false;
        bDone = false;
        placeInQueue = -1;
        this.bAButtonDown = zombie.GameWindow.ActivatedJoyPad != null && zombie.GameWindow.ActivatedJoyPad.isAPressed();
        zombie.SoundManager.instance.setMusicState("Loading");
        if (GameClient.bClient) {
            GameClient.instance.sendLoginQueueRequest2();
        }
    }

    @Override
    public GameState redirectState() {
        return (GameState)(bCancel ? new MainScreenState() : new GameLoadingState());
    }

    @Override
    public void render() {
        Core.getInstance().StartFrame();
        Core.getInstance().EndFrame();
        boolean _boolean = UIManager.useUIFBO;
        UIManager.useUIFBO = false;
        Core.getInstance().StartFrameUI();
        SpriteRenderer.instance.renderi(null, 0, 0, Core.getInstance().getScreenWidth(), Core.getInstance().getScreenHeight(), 0.0F, 0.0F, 0.0F, 1.0F, null);
        if (placeInQueue >= 0) {
            MainScreenState.instance.renderBackground();
            UIManager.render();
            ActiveMods.renderUI();
            ui.render();
        }

        Core.getInstance().EndFrameUI();
        UIManager.useUIFBO = _boolean;
    }

    @Override
    public GameStateMachine.StateAction update() {
        if (!GameClient.bClient) {
            return GameStateMachine.StateAction.Continue;
        } else {
            boolean _boolean = zombie.GameWindow.ActivatedJoyPad != null && zombie.GameWindow.ActivatedJoyPad.isAPressed();
            if (_boolean) {
                if (this.bAButtonDown) {
                    _boolean = false;
                }
            } else {
                this.bAButtonDown = false;
            }

            if (_boolean || Keyboard.isKeyDown(1) || !GameClient.instance.bConnected) {
                bCancel = true;
                zombie.SoundManager.instance.setMusicState("MainMenu");
                if (GameClient.connection != null) {
                    GameClient.instance.bConnected = false;
                    GameClient.bClient = false;
                    GameClient.connection.forceDisconnect("loading-queue-canceled");
                    GameClient.connection = null;
                }

                return GameStateMachine.StateAction.Continue;
            } else {
                return bDone ? GameStateMachine.StateAction.Continue : GameStateMachine.StateAction.Remain;
            }
        }
    }

    public static void onConnectionImmediate() {
        bDone = true;
    }

    public static void onPlaceInQueue(int place) {
        placeInQueue = place;
        ui.setPlaceInQueue(place);
    }
}
